package com.guat.dao;

import com.guat.entity.Article;
import com.guat.entity.User;
import com.guat.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ArticleDao {
    //获取所有文章
    public List<Article> getArticleAll() {
        Connection connection = DBUtil.getConnection();
        String sql = "SELECT * from `article` ";
        List<Article> list = new ArrayList<>();
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                Article article = new Article();
                article.setArticle_id(rs.getInt(1));
                article.setTitle(rs.getString(2));
                article.setContent(rs.getString(3));
                //2023-12-20 11:52:05.0使用字符串截取,把最后的.0去掉
                String str = rs.getString(4);
                String timeStr = str.substring(0, str.length() - 2); // 去掉最后两个字符
                //把改好的时间格式set进对象
                article.setCreation_time(timeStr);
                list.add(article);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    //添加文章
    public boolean addArticle(String title, String content) {
        Connection connection = DBUtil.getConnection();
        String sql = "insert into `article`(title,content) value('" + title + "','" + content + "')";
        try {
            Statement statement = connection.createStatement();
            //执行sql语句
            statement.execute(sql);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除文章
    public boolean deleteArticle(String id) {
        Connection connection = DBUtil.getConnection();
        String sql = "DELETE FROM `article` WHERE article_id = '" + id + "'";
        try {
            Statement statement = connection.createStatement();
            //执行sql语句
            statement.execute(sql);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    //根据id获取文章信息填入输入框
    public Article doUpdateArticle(String id) {
        Connection connection = DBUtil.getConnection();
        String sql = "SELECT * from `article` where article_id = '" + id + "'";
        try {
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            Article article = new Article();
            if (rs.next()) {
                article.setArticle_id(rs.getInt(1));
                article.setTitle(rs.getString(2));
                article.setContent(rs.getString(3));
                article.setCreation_time(rs.getString(4));
            }
            return article;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    //修改
    public void updateArticle(String id,String title,String content){
        Connection connection=DBUtil.getConnection();
        String sql="update `article` set title='"+title+"',content='"+content+"' where article_id="+id;
        try {
            Statement statement=connection.createStatement();
            statement.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //根据前端传回的值进行模糊查询
    public List<Article> searchArticle(String value) {
        Connection connection = DBUtil.getConnection();
        String sql = "SELECT * FROM article WHERE title LIKE ? OR content LIKE ?";
        List<Article> list = new ArrayList<>();
        try {
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, "%"+value+"%");
            statement.setString(2, "%"+value+"%");
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                Article article = new Article();
                article.setArticle_id(rs.getInt(1));
                article.setTitle(rs.getString(2));
                article.setContent(rs.getString(3));
                //2023-12-20 11:52:05.0使用字符串截取,把最后的.0去掉
                String str = rs.getString(4);
                String timeStr = str.substring(0, str.length() - 2); // 去掉最后两个字符
                //把改好的时间格式set进对象
                article.setCreation_time(timeStr);
                list.add(article);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }
}
